package me.pplay.playtalk.activity;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.net.ConnectivityManagerUtil;
import me.pplay.playtalk.net.SkyidOperation;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpAuth;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import me.pplay.playtalk.util.PlayTalkUtil;
import me.pplay.playtalk.util.UserInfo;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class SignInActivity extends BaseActivity implements OnClickListener{
	//View declare
	private Button signin_btn;
	private TextView signin_problem,signin_signup;
	private EditText userName_Et, password_Et;
	private ProgressDialog loadDialog;
	private TestBroadcastReceiver receiver;
	private TpAuth tpAuth;
	private String userName, password;
	private ProgressDialog progressDialog;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {				
			case PlayTalkUtil.PLAYTALK_LOGIN_OK:
				progressDialog.dismiss();
				SkyidOperation.saveSkyid(SignInActivity.this);
				SkyidOperation.savePassword(SignInActivity.this, password);
				SkyidOperation.saveUserName(SignInActivity.this, userName);
				PlayTalkActivity.palyTalkContext.finish();
				Intent intent = new Intent(SignInActivity.this,
						HomeActivity.class);  
				startActivity(intent);
				SignInActivity.this.finish();
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_ERR: {
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "SignIn Unkown err" : rspdata.getString());
			}
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_TIMEOUT:
				progressDialog.dismiss();
				showToast("SignIn Time out");
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_WAITING:
				break;
			default:
				break;
			}
		}

	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_signin);
		findViews();
		ApplicationTrans appState = (ApplicationTrans)getApplicationContext(); 
		tpAuth	= appState.getTpAuth();
		UserInfo.setContext(getApplicationContext());
	}
	
	/**
     * Get View by viewId 
     */
	private void findViews(){
		signin_btn		= (Button)findViewById(R.id.signin_btn);
		signin_problem	= (TextView)findViewById(R.id.signin_problem);
		signin_signup	= (TextView)findViewById(R.id.signin_signup);
		userName_Et		= (EditText)findViewById(R.id.userName_Et);
		password_Et		= (EditText)findViewById(R.id.password_Et);
		signin_btn.setOnClickListener(this);
		signin_problem.setOnClickListener(this);
		signin_signup.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.signin_btn:
			signIn();
			break;
		case R.id.signin_signup:
			signInSignUp();
			break;
		case R.id.signin_problem:
			signInProblem();
			break;
		default:
			break;
		}
	}

	private void signInProblem() {
		Intent intent = new Intent(SignInActivity.this, SigninGetPwdMethodActivity.class);
		startActivity(intent);
	}
	private void signInSignUp(){
		Intent intent = new Intent(SignInActivity.this,
				SignUpActivity.class);
		startActivity(intent);
	}
	private void signIn(){
		userName	= userName_Et.getText().toString().trim();
		password	= password_Et.getText().toString().trim();
		LogManager.LogShow("SignInActivity", "userName=" + userName + " password=" + password, LogManager.DEBUG);
		if(userName.equals("")){
			showToast(getString(R.string.username_isnull));
			return;
		} 
		if(password.equals("")){
			showToast(getString(R.string.password_isnull));
			return;
		}
//		showDialog();
		if(!ConnectivityManagerUtil.isAccessNetwork(this)){
			showToast(getString(R.string.noNet));
			return;
		}
		progressDialog = ProgressDialog.show(this, getString(R.string.loading), getString(R.string.waittng), true, false); 
		progressDialog.show();
		
		PlayTalkUtil.login((ApplicationTrans) getApplicationContext(), userName, password,  new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				Message msg = new Message(); 
				msg.what = result; 
				msg.obj = resultdata;
				if (PlayTalkUtil.PLAYTALK_LOGIN_OK == result) {
					msg.obj = obj;
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_OK Globals.skyid = " + Globals.skyid);
				} else if (PlayTalkUtil.PLAYTALK_LOGIN_ERR == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_ERR");
				} else if (PlayTalkUtil.PLAYTALK_LOGIN_TIMEOUT == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_TIMEOUT");
				} else if (PlayTalkUtil.PLAYTALK_LOGIN_WAITING == result) {
					msg.obj = obj;
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_WAITING");
				}
				handler.sendMessage(msg);
			}
		});

	}

	
//	private void showToast(String str){
//		Toast.makeText(SignInActivity.this, str, Toast.LENGTH_SHORT).show();
//	}

	@Override
	protected void onStart() {
		super.onStart();
		tpAuth.setContext(SignInActivity.this);
		receiver	= new TestBroadcastReceiver();
		IntentFilter filterIntent = new IntentFilter(TpAuth.BROADCASTACTION_TPAUTH);
		registerReceiver(receiver, filterIntent);
		
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unregisterReceiver(receiver);
	}
	
	class TestBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
		}}
}
